package cn.yeziji.forum.dto.question;

import cn.yeziji.forum.entity.ForumQuestionEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * 问题数据对象
 *
 * @author gzkemays
 * @since 2022/11/12 5:21 PM
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class QuestionDTO {
  Integer questionId;
  Integer answerId;
  String title;
  String content;

  public ForumQuestionEntity createEntity() {
    ForumQuestionEntity entity = new ForumQuestionEntity();
    if (StringUtils.isBlank(title)) {
      throw new RuntimeException("标题不能为空");
    }
    if (StringUtils.isBlank(content)) {
      throw new RuntimeException("内容不能为空");
    }
    entity.setTitle(title);
    entity.setContent(content);
    return entity;
  }

  public ForumQuestionEntity adoptedEntity() {
    ForumQuestionEntity entity = new ForumQuestionEntity();
    if (questionId == null || answerId == null) {
      throw new RuntimeException("数据不完整");
    }
    entity.setAnswerId(answerId);
    entity.setId(questionId);
    return entity;
  }
}
